package com.zcf.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zcf.common.json.Body;
import com.zcf.mapper.NoticMapper;
import com.zcf.pojo.Mall;
import com.zcf.pojo.Notic;
import com.zcf.pojo.User;
import com.zcf.vo.out.LayuiJson;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoQi
 * @since 2019-03-18
 */
@Service("NoticServiceImpl")
public class NoticServiceImpl {

	@Autowired
	NoticMapper nm;

	/**
	 * 获取公告
	 * @return
	 */
	public Body getnotic() {
		EntityWrapper<Notic> w = new EntityWrapper<Notic>();
		w.setSqlSelect("content");
		w.eq("id", 1);
		Integer num = nm.selectCount(w);
		Notic notic = new Notic();
		Notic notic1 = notic.selectOne(w);

		if (num == 0) {
			return Body.BODY_451;
		}
		return Body.newInstance(notic1);
	}

	/*
	 * 后端获取公告
	 */
	public LayuiJson getnotics(){
		LayuiJson lj = new LayuiJson();
		List<Notic> list = new ArrayList<Notic>();
		Notic notic = nm.selectById(1);
		if(notic != null){
			lj.setCode(0);
			lj.setCount(1);
			lj.setMsg("yes");
			list.add(notic);
			lj.setData(list);
			return lj;
		}
		lj.setCode(1);
		lj.setCount(0);
		lj.setMsg("no data");
		lj.setData(null);
		return lj;
	}
	/*
	 * 后端获取公告
	 */
	public LayuiJson getNotic(){
		LayuiJson lj = new LayuiJson();
		List<Notic> list = new ArrayList<Notic>();
		Notic notic = nm.selectById(2);
		if(notic != null){
			lj.setCode(0);
			lj.setCount(1);
			lj.setMsg("yes");
			list.add(notic);
			lj.setData(list);
			return lj;
		}
		lj.setCode(1);
		lj.setCount(0);
		lj.setMsg("no data");
		lj.setData(null);
		return lj;
	}

	/**
	 * 编辑公告
	 * @param notic
	 * @return
	 */
	public Body updatenotic(Notic notic) {
		boolean b = notic.updateById();
		if(b){
			return Body.BODY_200;
		}
		return Body.BODY_451;
	}
	/*
	 * 后端获取快捷用语
	 */
	public LayuiJson getNotics() {
		EntityWrapper<Notic> w = new EntityWrapper<Notic>();
		w.setSqlSelect("content","id");
		w.ge("id", 4);
		LayuiJson lj = new LayuiJson();
		List list = nm.selectPage(new Page<>(), w);
		Integer count = nm.selectCount(w);
		if (list != null) {
			lj.setCode(0);
			lj.setCount(count);
			lj.setData(list);
			lj.setMsg("成功");
			return lj;
		}
		lj.setCode(1);
		lj.setCount(count);
		lj.setData(null);
		lj.setMsg("暂无数据");
		return lj;
	}
	/**
	 * 添加快捷用语
	 * @param notic
	 * @return
	 */
	public Body add(Notic notic) {
		boolean b = false;
		if(notic.getContent() != null){
			b = notic.insert();
		}
		if(b){
			return Body.BODY_200;
		}
		return Body.BODY_400;
	}
	/**
	 * 删除快捷用语
	 * @param notic
	 * @return
	 */
	public Body delete(Notic notic) {
		if(notic.getId() != null ){
			boolean b = notic.deleteById();
			if(b){
				return Body.BODY_200;
			}
		}
		return Body.BODY_400;
	}
	/**
	 * 前端获取充值提示
	 * @return
	 */
	public Body getbuynotic() {
		EntityWrapper<Notic> w = new EntityWrapper<Notic>();
		w.setSqlSelect("content");
		w.eq("id", 2);
		Integer num = nm.selectCount(w);
		Notic notic = new Notic();
		Notic notic1 = notic.selectOne(w);

		if (num == 0) {
			return Body.BODY_451;
		}
		return Body.newInstance(notic1);
	}
	/**
	 * 前端获取快捷用语
	 * @return
	 */
	public Body getKnotics() {
		EntityWrapper<Notic> w = new EntityWrapper<Notic>();
		w.setSqlSelect("content");
		w.ge("id", 4);
		LayuiJson lj = new LayuiJson();
		List list = nm.selectPage(new Page<>(), w);
		if(list!=null){
			return Body.newInstance(list);
		}
		return Body.BODY_401;
	}
	/**
	 * 后端获取公告
	 * @return
	 */
	public LayuiJson getannouncement() {
		EntityWrapper<Notic> w = new EntityWrapper<Notic>();
		w.setSqlSelect("content","id");
		w.eq("id", 3);
		LayuiJson lj = new LayuiJson();
		List list = nm.selectPage(new Page<>(), w);
		Integer count = nm.selectCount(w);
		if (list != null) {
			lj.setCode(0);
			lj.setCount(count);
			lj.setData(list);
			lj.setMsg("成功");
			return lj;
		}
		lj.setCode(1);
		lj.setCount(count);
		lj.setData(null);
		lj.setMsg("暂无数据");
		return lj;
	}
	/**
	 * 后台获取消息公告
	 * @return
	 */
	public Body getmessage() {
		
		Notic notic = new Notic();
		Notic one = notic.selectOne(new EntityWrapper<Notic>().eq("id", 3));
		if(one != null){
			return Body.newInstance(one);
		}
		
		return Body.BODY_401;
	}

}
